package com.sc.highspeed.controller;

import com.sc.highspeed.constant.Constant;
import com.sc.highspeed.controller.result.JsonResult;
import com.sc.highspeed.service.ExcelHandleService;
import com.sc.highspeed.tools.PictureUpload;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Admin on 2017/12/8.
 */
@Controller
@RequestMapping("/upload")
@Api(value = "文件管理接口", description = "文件管理接口")
public class FileManageController {

    @Autowired
    private ExcelHandleService excelHandleService;

    @RequestMapping(value = "/excelUpload", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "excel表格上传",httpMethod = "POST",
            response = JsonResult.class, notes = "")
    public JsonResult excelUpload(@ApiParam(value = "文件") @RequestParam("file") CommonsMultipartFile file,
                                  @ApiParam(value = "表格类型(carInfo, esCarInfo)") @RequestParam String type) throws IOException{
        JsonResult result = new JsonResult();
        List<JsonResult> results = null;
        String fileName = file.getOriginalFilename();
        List<List<Object>> list = new ArrayList<List<Object>>();
        String formatString = fileName.substring(fileName.lastIndexOf("."));
        if (!formatString.equals(Constant.EXCEL_FORMAT_2003) && !formatString.equals(Constant.EXCEL_FORMAT_2007)) {
            result.setCode(0);
            result.setStatus(false);
            result.setMsg("文件格式有误！");
            return result;
        }

        InputStream in = file.getInputStream();

        results = excelHandleService.getListByExcel(in, fileName, type);

        result.setCode(1);
        result.setMsg("上传完毕！");
        result.setStatus(true);
        result.setObject(results);
        return result;
    }

    @RequestMapping(value = "/picUpload", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "图片上传",httpMethod = "POST",
            response = JsonResult.class, notes = "")
    public JsonResult picUpload(@ApiParam(value = "图片") @RequestParam CommonsMultipartFile file) {
        JsonResult result = new JsonResult();
        String url = "";
        if (file == null) {
            result.setCode(-1);
            result.setStatus(false);
            result.setMsg("图片不能为空！");
            return result;
        }
        try {
            url = PictureUpload.uploadPicture(file);
        } catch (Exception e) {
            result.setCode(0);
            result.setStatus(false);
            result.setMsg(e.getMessage());
            return result;
//            e.printStackTrace();
        }

        result.setCode(1);
        result.setStatus(true);
        result.setObject(url);
        return result;

    }

    @RequestMapping(value = "/download", method = RequestMethod.GET)
    @ApiOperation(value = "文件下载",httpMethod = "GET",
            response = JsonResult.class, notes = "")
    public ResponseEntity<byte[]> download(@ApiParam(value = "文件名(carInfo.xlsx, esCarInfo.xlsx, login.log, check.log)两份模板，两份日志(登录日志和审核日志)") @RequestParam String fileName) throws IOException {
        File file = null;
        if (fileName.equals("login.log")) {
            file = new File("C:/log/login.log");
        } else if (fileName.equals("check.log")) {
            file = new File("C:/log/check.log");
        } else if (fileName.equals("carInfo.xlsx")) {
            file = new File("C:/template/carInfo.xlsx");
        } else if (fileName.equals("esCarInfo.xlsx")) {
            file = new File("C:/template/esCarInfo.xlsx");
        }

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        httpHeaders.setContentDispositionFormData("attachment",fileName);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), httpHeaders, HttpStatus.CREATED);

    }

}
